//
//  VCModifyInfomation.swift
//  AirlineReservation
//
//  Created by Kevin on 2020/2/21.
//  Copyright © 2020 Kevin. All rights reserved.
//

import UIKit

class VCModifyInfomation: VCBase {
    
    private lazy var modifyInfomationView:ModifyInfomationView = {
        let ui = ModifyInfomationView()
        let uiY = realHeight + MARGIN10
        ui.frame = CGRect(x: 0, y: uiY, width: SCREEN_WIDTH, height: SCREEN_HEIGHT - uiY)
        return ui
    }()
    
    public var label:String?
    public var content:String?
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        init_view()
        init_data()
        init_event()
    }
}

extension VCModifyInfomation {
    
    // 修改用户信息
    private func modifyUserInfomation(request:NSDictionary) -> Void {
        self.doPost(api: "modify/user/infomation", dict: request) { (dict) in
            self.show(text: "修改成功")
            // 解析数据
            guard let result = dict.value(forKey: SP_BODYREQUEST) as? NSDictionary else {
                return
            }
            let userInfo = UserInfo(dict: result as! [String : Any])
            self.saveUserInfo(userInfo: userInfo)
            self.navigationController?.popViewController(animated: true)
        }
    }
    
    private func init_event() -> Void {
        modifyInfomationView.complete = {dict,isEmpty in
            if isEmpty {
                self.show(text: "输入内容不能为空")
                return
            }
            self.modifyUserInfomation(request: dict)
        }
    }
    
    private func init_data() -> Void {
        modifyInfomationView.resetLabel = label
        modifyInfomationView.resetContent = content
    }
    
    private func init_view() -> Void {
        view.addSubview(backroundWhite)
        view.addSubview(modifyInfomationView)
    }
}
